package com.yuyou.hxyy.gateway.controller;

import com.yuyou.hxyy.service.api.bean.*;
import com.yuyou.hxyy.service.api.entity.AuthUser;
import com.yuyou.hxyy.service.api.enums.UserEnums;
import com.yuyou.hxyy.service.api.mess.AuthUserMess;
import com.yuyou.hxyy.service.api.security.SecurityCons;
import com.yuyou.hxyy.service.api.service.IAuthUserService;
import com.yuyou.hxyy.service.api.service.IUserWordRepoService;
import com.yuyou.hxyy.service.api.util.DefindBeanUtils;
import com.yuyou.hxyy.service.api.util.Validator;
import com.yuyou.hxyy.service.api.vo.req.ReqUpdateAuthUser;
import com.yuyou.hxyy.service.api.vo.req.ReqUserPage;
import com.yuyou.hxyy.service.api.vo.resp.UserWordRepoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value="AuthUserController",description="用户接口")
@Validated
@RestController
@RequestMapping(value ="/authUser")
public class AuthUserController extends BaseController{

	@Autowired
	private IAuthUserService authUserService;

	@Autowired
	private IUserWordRepoService userWordRepoService;


	@ApiOperation(value="用户列表分页")
	@PreAuthorize(SecurityCons.admin)
	@RequestMapping(value="/findUserPage" ,method=RequestMethod.POST)
	public ReturnResult<AuthUser> findAuthUserPage(@Validated @ModelAttribute PageParams pageParams,
												   @Validated @ModelAttribute ReqUserPage reqUserPage){
		Map<String, Object> conditions=new HashMap<String, Object>();
		conditions.put("status", reqUserPage.getStatus());
		conditions.put("regTimeStart", reqUserPage.getRegTimeStart());
		conditions.put("regTimeEnd", reqUserPage.getRegTimeEnd());
		conditions.put("userType", UserEnums.userType.student.getValue());
		if(!StringUtils.isEmpty(reqUserPage.getKeyword())){
			conditions.put("keyword", reqUserPage.getKeyword().trim().replace("%", "\\%").replace("_","\\_"));
		}

		Pagination<AuthUser> queryPgn = new Pagination<AuthUser>();
		queryPgn.setConditions(conditions);
		queryPgn.setPageParams(pageParams);
		int total =authUserService.findAuthUserPageCount(queryPgn);
		queryPgn.setTotal(total);
		queryPgn.toBePage();
		if(total>0){
			queryPgn.setTotal(total);
			List<AuthUser> list=authUserService.findAuthUserPageList(queryPgn);
			queryPgn.setList(list);
		}
		return ReturnResult.returnRes(true, ReturnCode.code._1001.getMessage(),queryPgn);
	}

	@ApiOperation(value="用户状态更改")
	@PreAuthorize(SecurityCons.admin)
	@RequestMapping(value="/changeAuthUserStatus", method = { RequestMethod.GET ,RequestMethod.POST })
	public ReturnResult<String> changeAuthUserStatus(@NotEmpty(message="用户ids不能为空") @RequestParam(value = "ids") List<Long> ids,
									   		         @NotNull(message="状态status不能为空") @RequestParam(value = "status") Integer status){

		authUserService.batchChangeAuthUserStatus(ids, status);
		return ReturnResult.returnRes(true, ReturnCode.code._1001.getMessage(),null);
	}

	@ApiOperation(value="编辑用户")
	@RequestMapping(value="/updateAuthUser", method = { RequestMethod.GET ,RequestMethod.POST })
	public ReturnResult<AuthUser> updateAuthUser(@Validated @ModelAttribute ReqUpdateAuthUser reqUpdateAuthUser,
												 HttpServletRequest request){

		if(!StringUtils.isEmpty(reqUpdateAuthUser.getPhone()) && !Validator.isMobile(reqUpdateAuthUser.getPhone())){
			return ReturnResult.returnRes(false, AuthUserMess.PHONE_ERROR, null);
		}

		LoginRedisObj userObj = this.getUserObj(request);
		Integer userType = userObj.getUserType();

		AuthUser authUser = new AuthUser();
		DefindBeanUtils.copyProperties(reqUpdateAuthUser, authUser);
		if(0 == userType){
			authUser.setAuthUserId(userObj.getUserId());
		}
		authUser.setAccount(null);  // 帐号不能更改
		authUser.setPassword(null);
		authUserService.updateByPrimaryKeySelective(authUser);

		return ReturnResult.returnRes(true, ReturnCode.code._1001.getMessage(),authUser);
	}

	@ApiOperation(value="用户详情")
	@RequestMapping(value="/detailAuthUser", method = { RequestMethod.GET ,RequestMethod.POST })
	public ReturnResult<AuthUser> detailAuthUser(HttpServletRequest request){

		LoginRedisObj userObj = this.getUserObj(request);
		AuthUser authUser = authUserService.selectByPrimaryKey(userObj.getUserId());
		authUser.setPassword(null);

		return ReturnResult.returnRes(true, ReturnCode.code._1001.getMessage(),authUser);
	}

	@ApiOperation(value="用户学习状态")
	@RequestMapping(value="/userStuStatus", method = { RequestMethod.GET ,RequestMethod.POST })
	public ReturnResult<Integer> userStuStatus(HttpServletRequest request){

		LoginRedisObj userObj = this.getUserObj(request);
		List<UserWordRepoVo> repoList=userWordRepoService.findLearningList(userObj.getUserId(), 1);
		return ReturnResult.returnRes(true, ReturnCode.code._1001.getMessage(),repoList.size()>0?1:0);
	}



}
